use std::str::FromStr;
use std::env;

fn main() {
    println!("Hello, world!");

    test_match();

    let mut numbers = Vec::new();
    for arg in env::args().skip(1) {
        numbers.push( u64::from_str(&arg).expect("error parsing argument"));
    }

    if numbers.len() == 0 {
        eprintln!("Usage: gcd NUMBER ...");
        std::process::exit(1);
    }

    let mut d = numbers[0];
    for m in numbers.iter().skip(1) {
        d = gcd(d, *m);
    }
    println!("The greatest common divisor of {:?} is {}", numbers, d);
}

fn test_match() {
    let v = 98;

    match v {
        1 => println!("One"),
        2 => println!("Two"),
        val @ 3..=7 => println!("Three to seven: {}", val),
        n @ _ => println!("Something else, n={}", n),
    }
}

fn gcd(mut n :u64, mut m: u64) -> u64 {
    assert!(n != 0 && m != 0);
    while m != 0 {
        if m< n {
            let t = m;
            m = n;
            n = t;
        }
        m = m % n;
    }
    n
}

#[test]
fn test_gcd(){
    assert_eq!(gcd(14, 15), 1);
    assert_eq!(gcd(1*3*2*13*9*8*36, 1*3*5*7*11*13*37), 1*3*13);
}
